package com.ruoyi.web.controller.consume;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consume.domain.PesticideApplyTemporaryCon;
import com.ruoyi.consume.service.IPesticideApplyTemporaryConService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农药临时领用Controller
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Api("农药临时领用")
@RestController
@RequestMapping("/consume/pesticidetemap")
public class PesticideApplyTemporaryConController extends BaseController
{
    @Autowired
    private IPesticideApplyTemporaryConService pesticideApplyTemporaryService;

    /**
     * 查询农药临时领用列表
     */
    @ApiOperation("查询农药临时领用列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:list')")
    @GetMapping("/list")
    public TableDataInfo list(PesticideApplyTemporaryCon pesticideApplyTemporaryCon)
    {
        startPage();
        List<PesticideApplyTemporaryCon> list = pesticideApplyTemporaryService.selectPesticideApplyTemporaryList(pesticideApplyTemporaryCon);
        return getDataTable(list);
    }

    /**
     * 导出农药临时领用列表
     */
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:export')")
    @Log(title = "农药临时领用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PesticideApplyTemporaryCon pesticideApplyTemporaryCon)
    {
        List<PesticideApplyTemporaryCon> list = pesticideApplyTemporaryService.selectPesticideApplyTemporaryList(pesticideApplyTemporaryCon);
        ExcelUtil<PesticideApplyTemporaryCon> util = new ExcelUtil<PesticideApplyTemporaryCon>(PesticideApplyTemporaryCon.class);
        return util.exportExcel(list, "农药临时领用数据");
    }

    /**
     * 获取农药临时领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:query')")
    @GetMapping(value = "/{pesticideApplyTemporaryId}")
    public AjaxResult getInfo(@PathVariable("pesticideApplyTemporaryId") Long pesticideApplyTemporaryId)
    {
        return AjaxResult.success(pesticideApplyTemporaryService.selectPesticideApplyTemporaryById(pesticideApplyTemporaryId));
    }

    /**
     * 新增农药临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:add')")
    @Log(title = "农药临时领用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PesticideApplyTemporaryCon pesticideApplyTemporaryCon)
    {
        return toAjax(pesticideApplyTemporaryService.insertPesticideApplyTemporary(pesticideApplyTemporaryCon));
    }

    /**
     * 修改农药临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:edit')")
    @Log(title = "农药临时领用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PesticideApplyTemporaryCon pesticideApplyTemporaryCon)
    {
        return toAjax(pesticideApplyTemporaryService.updatePesticideApplyTemporary(pesticideApplyTemporaryCon));
    }

    /**
     * 删除农药临时领用
     */
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:remove')")
    @Log(title = "农药临时领用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pesticideApplyTemporaryIds}")
    public AjaxResult remove(@PathVariable Long[] pesticideApplyTemporaryIds)
    {
        return toAjax(pesticideApplyTemporaryService.deletePesticideApplyTemporaryByIds(pesticideApplyTemporaryIds));
    }

    /**
     * 更新库存量表
     */
    @ApiOperation("更新库存量表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:amount')")
    @Log(title = "农药临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/amount")
    public AjaxResult amount(@RequestBody PesticideApplyTemporaryCon pesticideApplyTemporaryCon)
    {
        int row = pesticideApplyTemporaryService.updateAmount(pesticideApplyTemporaryCon);
        if(row == 0){
            return error("库存不足");
        }
        else {
            return toAjax(row);
        }
    }

    /**
     * 更新领用状态
     */
    @ApiOperation("更新领用状态")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:consu')")
    @Log(title = "农药临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/consu/{pesticideApplyTemporaryId}")
    public AjaxResult consume(@PathVariable("pesticideApplyTemporaryId") Long pesticideApplyTemporaryId)
    {
        return toAjax(pesticideApplyTemporaryService.updateConsumeStatus(pesticideApplyTemporaryId));
    }

    /**
     * 退回操作
     */
    @ApiOperation("退回操作")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:pesticidetemap:back')")
    @Log(title = "农药临时领用", businessType = BusinessType.UPDATE)
    @PutMapping("/back/{pesticideApplyTemporaryId}")
    public AjaxResult back(@PathVariable("pesticideApplyTemporaryId") Long pesticideApplyTemporaryId)
    {
        return toAjax(pesticideApplyTemporaryService.updateApprovalStatus(pesticideApplyTemporaryId));
    }
}
